package com.leetroll.media.service;

import com.leetroll.api.service.FileService;
import com.leetroll.common.entity.ChapterDetail;
import com.leetroll.common.enums.EBucket;
import com.leetroll.common.enums.EFileType;
import com.leetroll.common.utils.AliyunOSSUtil;
import com.leetroll.media.utils.AboutFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static com.leetroll.common.constant.AliyunConstant.BUCKET;
import static com.leetroll.common.utils.AliyunOSSUtil.GetUrl;

@Component
@Slf4j
public class UpDownloadService {
    private final FileService fileService;

    @Autowired
    public UpDownloadService(FileService fileService) {
        this.fileService = fileService;
    }

    public void upload2AliByType(Long itemId, File file, EFileType fileType) {
        //插入主图文件
        String path = "item/" + itemId + "/" + fileType.getType() + "/";
        try {
            AliyunOSSUtil.upload(path + file.getName(), Files.newInputStream(file.toPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        saveFile(itemId, file, fileType, path, null);
    }

    public String uploadChapterMain2Ali(Long itemId, Long chapterId, File file, EFileType fileType) {
        String path = "item/" + itemId + "/" + EFileType.CHAPTER.getType() + "/" + chapterId + "/" + fileType.getType() + "/";
        try {
            AliyunOSSUtil.upload(path + file.getName(), Files.newInputStream(file.toPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return GetUrl(path, 30);
    }

    public String uploadChapterVideo2Ali(Long itemId, Long chapterId, File file, EFileType fileType, String seq, String title, String format, Long duration) {
        String path = "item/" + itemId + "/" + EFileType.CHAPTER.getType() + "/" + chapterId + "/" + fileType.getType() + "/" + seq + "/";
        log.info(path);
        /*try {
            String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            AliyunOSSUtil.uploadVideo(path + file.getName(), Files.newInputStream(file.toPath()), suffix);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
       /* if (file.getName().endsWith("m3u8")) {
            com.leetroll.common.entity.File savedFile = saveFile(itemId, file, fileType, path, format);
            ChapterDetail detail = new ChapterDetail();
            detail.setChapterId(chapterId);
            detail.setFileId(savedFile.getId());
            detail.setTitle(title);
            detail.setSeq(Integer.parseInt(seq));
            detail.setDuration(duration);
            detail.insert();
            log.info("chapter[{}]m3u8文件插入完成 > {}", chapterId, path);
        }*/
        return null;
    }

    private com.leetroll.common.entity.File saveFile(Long itemId, File file, EFileType fileType, String path, String format) {
        com.leetroll.common.entity.File LFile = new com.leetroll.common.entity.File();
        String name = file.getName().substring(0, file.getName().lastIndexOf("."));
        String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
        LFile.setName(name);
        LFile.setFormat(format);
        if (format == null) {
            LFile.setFormat(suffix);
        }
        LFile.setSuffix(suffix);
        LFile.setType(fileType.getType());
        LFile.setBucket(BUCKET);
        LFile.setKey(path);
        LFile.setSize(file.length());
        LFile.setRescId(itemId);
        fileService.save(LFile);
        return LFile;
    }

}
